package app.qrcode.browser.caculationtest

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.lifecycle.SavedStateViewModelFactory
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.NavController
import androidx.navigation.Navigation
import app.qrcode.browser.caculationtest.databinding.FragmentQuestionBinding

// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"

/**
 * A simple [Fragment] subclass.
 * Use the [QuestionFragment.newInstance] factory method to
 * create an instance of this fragment.
 */
class QuestionFragment : Fragment() {
    // TODO: Rename and change types of parameters
    private var param1: String? = null
    private var param2: String? = null


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            param1 = it.getString(ARG_PARAM1)
            param2 = it.getString(ARG_PARAM2)
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        val myViewModel = ViewModelProvider(
            requireActivity(),
            SavedStateViewModelFactory(requireActivity().application, this)
        ).get(MyViewModel::class.java)
        myViewModel.generator()
        val binding = DataBindingUtil.inflate<FragmentQuestionBinding>(
            inflater,
            R.layout.fragment_question,
            container,
            false
        )
        binding.data = myViewModel
        binding.lifecycleOwner = (requireActivity())
        val builder = StringBuilder()
        fun setNumClicker(vararg btns: Button) {
            btns.forEach { btn ->
                btn.setOnClickListener {
                    builder.append(btn.text)
                    if (builder.isEmpty()) {
                        binding.textView13.setText(R.string.input_indicator)
                    } else {
                        binding.textView13.text = builder.toString()
                    }
                }
            }
        }
        setNumClicker(
            binding.button0,
            binding.button1,
            binding.button2,
            binding.button3,
            binding.button4,
            binding.button5,
            binding.button6,
            binding.button7,
            binding.button8,
            binding.button9
        )
        binding.buttonClear.setOnClickListener {
            builder.setLength(0)
            if (builder.isEmpty()) {
                binding.textView13.setText(R.string.input_indicator)
            } else {
                binding.textView13.text = builder.toString()
            }
        }
        binding.buttonSubmit.setOnClickListener {
            Log.w("cdm", "QuestionFragment.onCreateView -> ${myViewModel.getAnswer().value}")
            if (builder.toString().equals("")) {
                val navController: NavController = Navigation.findNavController(it)
                navController.navigate(R.id.action_questionFragment_to_loseFragment2)
            } else {
                if (builder.toString().toInt() == myViewModel.getAnswer().value) {
                    myViewModel.answerCorrect()
                    builder.setLength(0)
                    binding.textView13.setText(getString(R.string.answer_correct_message))
                } else {
                    val navController: NavController = Navigation.findNavController(it)
                    if (myViewModel.winFlag) {
                        navController.navigate(R.id.action_questionFragment_to_winFragment)
                        myViewModel.winFlag = false
                        myViewModel.save()
                    } else {
                        navController.navigate(R.id.action_questionFragment_to_loseFragment2)
                    }
                }
            }
        }
        return binding.root

    }



    companion object {
        /**
         * Use this factory method to create a new instance of
         * this fragment using the provided parameters.
         *
         * @param param1 Parameter 1.
         * @param param2 Parameter 2.
         * @return A new instance of fragment QuestionFragment.
         */
        // TODO: Rename and change types and number of parameters
        @JvmStatic
        fun newInstance(param1: String, param2: String) =
            QuestionFragment().apply {
                arguments = Bundle().apply {
                    putString(ARG_PARAM1, param1)
                    putString(ARG_PARAM2, param2)
                }
            }
    }
}